﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gollie.DataTables;
using Gollie.Extensions;

namespace Gollie.Controls
{
	public partial class VendorViewReviews : AuthenticationRequiredAdministratorBaseUserControl
	{
		public static System.Drawing.Color errorColor = System.Drawing.Color.FromArgb(253, 81, 77);

		private DateTime from;
		private DateTime to;

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.Populate();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.Initialize();
			this.Populate();
		}

		private void Populate()
		{
			if (!this.ValidatePeriod())
				return;

			Reviews reviews = new Reviews(UserInfo.VendorId, from, to.AddDays(1));

			this.PopulateHeader(reviews);
			this.PopulateTable(reviews);
			this.PopulatePercentageBars(reviews);
		}

		private void PopulateHeader(Reviews reviews)
		{
			this.lblAvgRating.Text = string.Format("{0:0.0}", reviews.AverageRating);
			this.lblNumberOfReviews.Text = reviews.Count.ToString();
		}

		private void PopulateTable(Reviews reviews)
		{
			bool r = reviews.Count > 0;
			this.pnlNoReviews.Visible = !r;
			this.pnlToggleAll.Visible = r;

			if (reviews.Count == 0)
				return;

			foreach (ReviewsItem item in reviews)
			{
				TableRow row = new TableRow();

				row.Cells.Add(CreateDateCell(item.ReviewDate));
				row.Cells.Add(CreateRatingCell(item.Rating));
				row.Cells.Add(CreateCommentCell(item.Comment));

				this.tblRatings.Rows.Add(row);
			}
		}

		private void PopulatePercentageBars(Reviews reviews)
		{
			for (int i=4; i>=0; i--)
			{
				PercentageBar p = this.LoadControl("PercentageBar.ascx") as PercentageBar;
				p.MaxValue = (uint)reviews.Count;
				p.Value = reviews.Ratings[i];
				p.Stars = i + 1;
				this.pnlPercentageBars.Controls.Add(p);
			}
		}

		private TableCell CreateDateCell(DateTime dt)
		{
			TableCell cellDate = CreateCell();
			cellDate.Text = dt.ToString("yyyy-MM-dd, HH:MM");

			return cellDate;
		}

		private TableCell CreateRatingCell(uint rating)
		{
			TableCell cellRating = CreateCell();

			for (int i = 0; i < rating; i++)
			{
				Image star = new Image();
				star.ImageUrl = "/Images/star-on.png";
				cellRating.Controls.Add(star);
			}

			return cellRating;
		}

		private TableCell CreateCommentCell(string comment)
		{
			TableCell cellComment = CreateCell();
			cellComment.CssClass += " comment";

			if (string.IsNullOrEmpty(comment) == false)
			{
				cellComment.Controls.Add(CreateExpandCollapseImage());
				cellComment.Controls.Add(CreateFullCommentTextBox(comment));
				cellComment.Controls.Add(CreateAbbreviatedCommentLabel(comment));
			}
			else
			{
				cellComment.Text = "-";
			}

			return cellComment;
		}

		private Image CreateExpandCollapseImage()
		{
			Image imgExpand = new Image();
			imgExpand.ImageUrl = "/Images/expand.png";
			imgExpand.CssClass = "expandcollapse";

			return imgExpand;
		}

		private TextBox CreateFullCommentTextBox(string comment)
		{
			TextBox tb = new TextBox();
			tb.TextMode = TextBoxMode.MultiLine;
			tb.Text = comment;
			tb.CssClass = "fullcomment";
			tb.Enabled = false;

			return tb;
		}

		private Label CreateAbbreviatedCommentLabel(string comment)
		{
			string abbrev = comment.Clean();
			Label lblAbbrevComment = new Label();
			lblAbbrevComment.CssClass = "abbrevcomment";
			if (abbrev.Length < 32)
				lblAbbrevComment.Text = abbrev;
			else
				lblAbbrevComment.Text = abbrev.Substring(0, 32) + "...";

			return lblAbbrevComment;
		}

		private TableCell CreateCell()
		{
			return new TableCell() { CssClass = "vendorview" };
		}

		private void Initialize()
		{
			DateTime toDay = DateTime.Today;
			DateTime firstOfPreviousMonth = toDay.AddMonths(-1).AddDays(-(toDay.Day - 1));
			this.tbDateFrom.Text = ObjectFormatter.GetShortDateString(firstOfPreviousMonth);
			this.tbDateTo.Text = ObjectFormatter.GetShortDateString(DateTime.Today);
		}

		private bool ValidatePeriod()
		{
			bool ok = true;

			if (DateTime.TryParse(this.tbDateFrom.Text, out from) == false)
			{
				ok = false;
				this.tbDateFrom.BackColor = errorColor;
			}
			else
				this.tbDateFrom.BackColor = System.Drawing.Color.White;

			if (DateTime.TryParse(this.tbDateTo.Text, out to) == false)
			{
				ok = false;
				this.tbDateTo.BackColor = errorColor;
			}
			else
				this.tbDateTo.BackColor = System.Drawing.Color.White;

			if (from > to)
			{
				ok = false;
				this.tbDateFrom.BackColor = errorColor;
				this.tbDateTo.BackColor = errorColor;
			}

			return ok;
		}
	}
}